import shapefile
from datetime import date

# 文本字段使用'C'类型创建，第三个'size'参数可以自定义为文本值的预期长度以节省空间
# w = shapefile.Writer("shapefiles")
# w.field('TEXT', 'C')
# w.field('SHORT_TEXT', 'C', size=5)
# w.field('LONG_TEXT', 'C', size=250)
# w.null()
# w.record('Hello', 'World', 'World'*50)
# w.close()
#
# r = shapefile.Reader("shapefiles")
# print(len(r))
# print(r.shapeTypeName)
# r.close()



# 日期字段使用'D'类型创建，并且可以使用日期对象，列表或YYYYMMDD格式的字符串创建
w = shapefile.Writer('shapefiles')
w.field('DATE', 'D')
w.null()
w.null()
w.null()
w.null()
w.record(date(1898, 1, 29))
w.record([1998, 1, 30])
w.record('19980131')
w.record(None)
w.close()

r = shapefile.Reader('shapefiles')
shapeRecs = r.shapeRecords()
print(shapeRecs[0].record[0])
print(shapeRecs[1].record[0])
print(shapeRecs[2].record[0])
print(shapeRecs[3].record[0])
assert r.record(0) == [date(1898,1,29)]
assert r.record(1) == [date(1998,1,30)]
assert r.record(2) == [date(1998,1,31)]
assert r.record(3) == [None]



# 可以使用关键字参数添加属性，其中关键字是字段名称
# w = shapefile.Writer('shapefiles')
# w.field('FIRST_FLD','C','40')
# w.field('SECOND_FLD','C','40')
# w.null()
# w.null()
# w.record('First', 'Line')
# w.record(FIRST_FLD='First', SECOND_FLD='Line')
# w.close()